<?php
class IndexAction extends CommonAction {

	// 框架首页
	public function index() {
	   
	    $resource=M('resource');

		if(empty($_SESSION['MENU']))
		{
		if($_SESSION['permission']=='all')
		{
		$menu=$resource->where(array('pid'=>0))->select();
		if($menu){
		foreach($menu as $key=>$val){
		$sub=$resource->where(array('pid'=>$val['id']))->select();
		if($sub)
		$menu[$key]['sub']=$sub;
		}
		}
		}
		else
		{
		$pid=$resource->field('pid')->where("id in(".$_SESSION['permission'].") ")->group('pid')->select();
		$sPid=array();
		foreach($pid as $v)
		{
		$sPid[]=$v['pid'];
		}
		 $menu=$resource->where("id in(".implode(',',$sPid).")")->select();
		 foreach($menu as $key=> $val){
		 $sub=$resource->where("pid='".$val['id']."' and id in(".$_SESSION['permission'].") ")->select();
		 if($sub)
		 $menu[$key]['sub']=$sub;
		 }
		}		
		  $_SESSION['MENU']=$menu;
		}
		if($_SESSION['admin_user']!='admin')
		{
		$model=new Model;
		$gres=$model->query("SELECT a.`name` from think_role a left join think_role_user b on a.id=b.role_id left join think_user c on b.user_id=c.id where c.account='".$_SESSION['admin_user']."'");
		$gres=array_reduce($gres, function(&$v,$a){
		$v[]=$a['name'];
		return $v;
		});
		}
		else
		$gres=array('超级管理员');
		//print_r($_SESSION);
		C ( 'SHOW_RUN_TIME', false ); // 运行时间显示
		C ( 'SHOW_PAGE_TRACE', false );
		$this->assign("groupName",implode(',',$gres));
		$this->assign("menu",$_SESSION['MENU']);
		$this->display ();
	}

}
?>